/****************************************************
    作者："You_Want"
    日期："2020-05-01 23:40:08"
    功能：Nothing
*****************************************************/
using System.Collections;
using System.Collections.Generic;


/// <summary>
///  数据管理基类
/// </summary>
public abstract class AbstractDBModel<T,P>
    where T : class,new()
    where P : AbstractEntity   
{
    protected List<P> m_List;
    protected Dictionary<int, P> m_Dic;

    public AbstractDBModel()
    {
        m_List = new List<P>();
        m_Dic = new Dictionary<int, P>();
        LoadData();

    }

    #region 单例
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new T();
                //instance.Load();
            }
            return instance;
        }
    }
    #endregion

    #region  需要子类实现的属性或方法
    /// <summary>
    /// 数据文件名称 (待子类赋值，从子类身上获取 FileName)
    /// </summary>
    protected abstract string FileName { get; }

    /// <summary>
    /// 创建实体（从子类身上获取属性）
    /// </summary>
    /// <param name="parse"></param>
    /// <returns></returns>
    protected abstract P MakeEntity(GameDataTableParser parse);
    #endregion

    #region LoadData 加载数据
    /// <summary>
    /// 加载data文件数据
    /// </summary>
    private void LoadData()
    {
        using (GameDataTableParser parse = new GameDataTableParser( string.Format(@"C:\003_Data\003_GameData\{0}", FileName)))   //FileName 从子类身上获取
        {
            while (!parse.Eof) 
            { 
                //创建实体
                P p = MakeEntity(parse);
                m_List.Add(p);      //将实体加到列表
                m_Dic[p.Id] = p;    //将实体加到字典
                parse.Next();
            }
        }
    }
    #endregion

    #region 获取集合 GetList
    /// <summary>
    /// 获取集合
    /// </summary>
    /// <returns></returns>
    public List<P> GetList()
    {
        return m_List;
    }
    #endregion

    #region Get 根据编号获取实体 Entity
    /// <summary>
    /// 根据编号获取实体  Entity
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public P Get(int id)
    {
        if (m_Dic.ContainsKey(id))
        {
            return m_Dic[id];
        }
        return null;
    }
    #endregion
}
